package com.an.SpringMvcl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

import com.an.intecepter.TimeIntecepter;
import com.an.message.conv.MyMessageConvert;

/**
 *
* @ClassName: SpringMvcConfig
* @Description: TODO(Spring MVC 配置类)
* @author antch,86171 /g'b0865@qq.com
* @date 2017年7月14日 上午10:41:23
*
 */

@Configuration
@EnableWebMvc//开启springmvc支持，若无此重写WebMvcConfigurerAdapter方法无效
@ComponentScan("com.an")
/*
 * 继承WebMvcConfigurerAdapter类，重写其方法可对Spring Mvc进行配置
 *
 */
public class SpringMvcConfig extends WebMvcConfigurerAdapter {

	/*
	 * 自定义参数转换
	 */
	@Autowired
	private MyMessageConvert myMessageConvert;

	/*
	 * 处理时间统计
	 */
	@Autowired
	private TimeIntecepter timeIntecepter;

	/*
	 * (非 Javadoc)
	* <p>Title: extendMessageConverters</p>
	* <p>Description: SpringMvc里面注册HTTPMessageConvert</p>
	* @param converters
	* @see org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter#extendMessageConverters(java.util.List)
	 */
	@Override
	public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
		// TODO Auto-generated method stub
		converters.add(myMessageConvert);
	}

	/**
	 *
	* @Title: multipartResolver
	* @Description: TODO(文件上传解析)
	* @param @return    设定文件
	* @return MultipartResolver    返回类型
	* @throws
	 */
	@Bean
	public MultipartResolver  multipartResolver() {
		CommonsMultipartResolver multipartResolver =
				new CommonsMultipartResolver();
		multipartResolver.setMaxUploadSize(1024*1024*1024);
		//解决文件名乱码
		multipartResolver.setDefaultEncoding("UTF-8");
		return multipartResolver;
	}

	/*
	 * 视图解析
	 */
	@Bean
	public InternalResourceViewResolver viewResolver() {
		InternalResourceViewResolver viewResolver =
				new InternalResourceViewResolver();
		viewResolver.setPrefix("/WEB-INF/classes/views/");
		viewResolver.setSuffix(".jsp");
		viewResolver.setViewClass(JstlView.class);
//		viewResolver.set
		return viewResolver;
	}


	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		// TODO Auto-generated method stub
		/*
		 * addResourceHandler指的是对外暴露的访问路径
		 * addResourceLocations指的是放置文件的目录
		 */
		registry.addResourceHandler("/static-r/**").addResourceLocations("classpath:/static-r/");
	}

	/*
	 * (非 Javadoc)
	* <p>Title: addInterceptors</p>
	* <p>Description: 添加拦截器</p>
	* @param registry
	* @see org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter#addInterceptors(org.springframework.web.servlet.config.annotation.InterceptorRegistry)
	 */
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		// TODO Auto-generated method stub
		registry.addInterceptor(timeIntecepter);
	}

	/*
	 * (非 Javadoc)
	* <p>Title: addViewControllers</p>
	* <p>Description: controller转向页面的配置</p>
	* @param registry
	* @see org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter#addViewControllers(org.springframework.web.servlet.config.annotation.ViewControllerRegistry)
	 */
	@Override
	public void addViewControllers(ViewControllerRegistry registry) {

		//不用直接输入jsp页面地址，而是输入类似action的地址完成页面的跳转
		registry.addViewController("/toUpload").setViewName("/upload");

		//参数转换映射演示页面
		registry.addViewController("/converter").setViewName("/convert");

		//tian添加转向see.jsp的映射。这个主要用来完成SSE的支持
		registry.addViewController("/sse").setViewName("/sse");
	}

}
